//
//  LYFullCommentNewCell.m
//  LYLiveApp
//
//  Created by QD on 2018/3/16.
//  Copyright © 2018年 览益信息科技. All rights reserved.
//

#import "LYFullCommentNewCell.h"
#import "LYCommentModel.h"
#import "LYUtil.h"
#import "VideoViewContants.h"

@interface LYFullCommentNewCell ()
@property (weak, nonatomic) IBOutlet UIView *backgdView;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

@end

@implementation LYFullCommentNewCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"LYFullCommentNewCell";
    LYFullCommentNewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"LYFullCommentNewCell" owner:self options:nil] lastObject];
    }
    return cell;
}

- (void)setCommentModel:(LYCommentModel *)commentModel {
    
    _commentModel = commentModel;
    
    NSString *contentString = @"";
    NSString *nameString = @"";
    if (commentModel.show.user.nickname.length == 0) {
        commentModel.show.user.nickname = @"览益";
    }
    if (commentModel.show.user.nickname.length == 0 || commentModel.show.content.length == 0) {
        self.height = 0;
        return;
    }
    if (commentModel.show.user.nickname.length <= 7) {
        nameString = [commentModel.show.user.nickname stringByAppendingString:@": "];
    } else {
        NSString *string1 = [commentModel.show.user.nickname substringToIndex:2];
        NSString *string2 = [commentModel.show.user.nickname substringFromIndex:commentModel.show.user.nickname.length - 4];
        nameString = [nameString stringByAppendingFormat:@"%@...%@: ",string1,string2];
    }
    commentModel.show.content = [commentModel.show.content stringByReplacingOccurrencesOfString:@" " withString:@""];

    contentString = [nameString stringByAppendingString:commentModel.show.content];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[contentString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang-SC-Regular" size:15.0f] range:NSMakeRange(0, [attributedString length])];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, [attributedString length])];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:kVideoCommentCellContentLineSpacing];
    if (commentModel.show.shares.count)
    {
        for (NSString *name in commentModel.show.shares)
        {
            NSRange range = [attributedString.string rangeOfString:name];
            if (range.location >= 0 && range.location != NSNotFound && range.length >0)
            {
                [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#4990e2"] range:range];
                [attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang-SC-Semibold" size:15.0f] range:range];
            }
        }
    }
    
    NSRange nameRange = [attributedString.string rangeOfString:nameString];
    if (nameRange.location >= 0 && nameRange.location != NSNotFound && nameRange.length >0)
    {
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#d94b41"] range:nameRange];
        [attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang-SC-Medium" size:15.0f] range:nameRange];

    }
    
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
    self.contentLabel.attributedText = attributedString;
    
    self.contentLabel.height = [LYUtil sizeWithText:contentString
                                               font:[UIFont fontWithName:@"PingFangSC-Regular" size:kVideoFullScrCommentCellContentFontSize]
                                              width:kVideoFullScrCommentCellContentMaxWidth
                                        lineSpacing: kVideoCommentCellContentLineSpacing].height;

    self.height = [LYCommentModel returnHeightFullCommentNewWithModel:commentModel];
    
  
    self.backgdView.height = self.height - 10;    
    
    CGFloat maxWidth = 0;
    CGFloat contentStrWidth = [LYUtil sizeWithText:contentString
                                              font:[UIFont fontWithName:@"PingFangSC-Regular" size:kVideoFullScrCommentCellContentFontSize]
                                        withHeight:kVideoFullScrCommentLabelOriginHeight].width;
    maxWidth = contentStrWidth;
    if (maxWidth > 270) {
        maxWidth = 270;
    } else if (maxWidth < 260) {
        maxWidth = contentStrWidth +10;
    }
    
    self.backgdView.width = maxWidth + 20;
}

@end
